package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.CinemaBackMapper;
import com.zretc.entity.Cinema;
import com.zretc.entity.Movie;
import com.zretc.service.CinemaBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 乔志宇
 * @date 2020-7-26
 */
@Service
public class CinemaBackServiceImpl implements CinemaBackService {

    @Autowired
    private CinemaBackMapper cinemaBackMapper;

    @Override
    public List<Cinema> getCinema() {
        List<Cinema> cinemas = cinemaBackMapper.findCinema();
        return cinemas;
    }

    @Override
    public PageInfo<Movie> getMovieByCinemaAndMovieName(Map<String, String> params) {

        // 获取当前页数
        int pageNum = Integer.parseInt(params.get("pageNum"));
        // 获取条数
        int pageSize = Integer.parseInt(params.get("pageSize"));
        // 获取电影院名
        String cinemaName = params.get("cinemaName");
        // 获取电影名
        String movieName = params.get("movieName");

        // 进行分页
        PageHelper.startPage(pageNum,pageSize);

        // 调用dao方法
        // 对传递给dao方法中参数进行处理
        Map<String,String> map = new HashMap<>();

        if ("".equals(movieName) || movieName == null){
            map.put("movieName",null);
        }else{
            map.put("movieName","%" + movieName + "%");
        }

        if ("".equals(cinemaName) || cinemaName == null){
            map.put("cinemaName",null);
        }else{
            map.put("cinemaName", cinemaName);
        }

        List<Movie> movieByCinemaAndMovieName = cinemaBackMapper.findMovieByCinemaAndMovieName(map);

        PageInfo<Movie> pageInfo = new PageInfo<>(movieByCinemaAndMovieName);

        return pageInfo;
    }
}
